package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.domain.User;
import com.powernode.model.R;
import com.powernode.model.SecurityUser;
import com.powernode.service.UserService;
import com.powernode.util.AuthUtils;
import com.powernode.vo.UserDetailInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口管理
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 查询用户信息
     * @return
     */
//    @PreAuthorize("hasAuthority('user:view')")
    @GetMapping("/info")
    public R<SecurityUser> loadUserInfo() {
        // 获取登录用户ID
        Integer userId = AuthUtils.getLoginUserId();
        // 根据用户ID查询用户信息
        // User user = userService.queryUserById(userId);
        return R.OK(AuthUtils.getSecurityUser());
    }

    /**
     * 查询用户详细信息
     * @return
     */
//    @PreAuthorize("hasAuthority('user:view')")
    @GetMapping("/login/detail/info")
    public R<UserDetailInfoVo> queryUserLoginDetailInfo() {
        // 根据用户标识查询用户详细信息
        UserDetailInfoVo userDetailInfoVo = userService.queryUserDetailInfoById(AuthUtils.getLoginUserId());
        return R.OK(userDetailInfoVo);
    }


    @GetMapping("/checkLoginPwd")
    public R<Boolean> checkLoginPwd(@RequestParam String oldPwd) {
        // 获取登录用户对象
        SecurityUser securityUser = AuthUtils.getSecurityUser();
        return R.OK(passwordEncoder.matches(oldPwd, securityUser.getPassword()));
    }

    @PreAuthorize("hasAuthority('user:edit')")
    @PutMapping("/password")
    public R<String> modifyUserLoginPwd(@RequestBody User user) {
        Boolean modified = userService.modifyUserLoginPwd(user);
        return R.HANDLER(modified);
    }

    @GetMapping("/isLogin")
    public R<String> isLogin() {
        return R.OK(null);
    }

    /**
     * 分页查询用户列表
     * @param current
     * @param size
     * @return
     */
//    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/page")
    public R<PageInfo<User>> queryUserPage(@RequestParam Integer current,
                                           @RequestParam(defaultValue = "10") Integer size) {
        PageInfo<User> pageInfo = userService.queryUserListByPage(current,size);
        return R.OK(pageInfo);
    }

    /**
     * 根据用户标识查询用户详情
     * @param userId
     * @return
     */
    @GetMapping("/detail/info/{userId}")
    public R<UserDetailInfoVo> queryUserDetailInfo(@PathVariable Integer userId) {
        // 根据用户标识查询用户详细信息
        UserDetailInfoVo userDetailInfoVo = userService.queryUserDetailInfoById(userId);
        return R.OK(userDetailInfoVo);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping
    public R<String> saveUser(@RequestBody User user) {
        Boolean saved = userService.saveUser(user);
        return R.HANDLER(saved);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping
    public R<String> modifyUser(@RequestBody User user) {
        Boolean modified = userService.modifyUser(user);
        return R.HANDLER(modified);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public R<String> removeUser(@PathVariable Integer userId) {
        Boolean removed = userService.removeUserById(userId);
        return R.HANDLER(removed);
    }

    /**
     * 批量删除用户
     * @param userIds
     * @return
     */
    @DeleteMapping("/batch/{userIds}")
    public R<String> batchRemoveUsers(@PathVariable List<Integer> userIds) {
        Boolean removed = userService.batchRemoveUsersByUserIds(userIds);
        return R.HANDLER(removed);
    }

    /**
     * 查询所有用户列表
     * @return
     */
    @GetMapping("/list")
    public R<List<User>> loadAllUsers() {
        List<User> users = userService.queryAllUsers();
        return R.OK(users);
    }
}
